package cm.mefobe.android.telecomcost.view;

import java.security.InvalidParameterException;

import cm.mefobe.android.telecomcost.entities.Destination;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TableLayout;

public class DestinationView extends TableLayout {

	protected Context _context;
	protected ZonePickerView _zonePickerView;
	protected CountryPickerView _countryPickerView;
	protected ProviderPickerView _providerPickerView;

	public DestinationView(Context context) {
		super(context);
		Log.i("DestinationView", "creating the DestinationView");
		_context = context;
	}

	protected void setDestinationView() {
		if (isInEditMode()) {
			Log.i("DestinationView", "isInEditMode");
		} else {
			Log.i("DestinationView", "adding views...");

			_zonePickerView = new ZonePickerView(_context);
			_countryPickerView = new CountryPickerView(_context);
			_providerPickerView = new ProviderPickerView(_context);

			_countryPickerView.getCountrySpinner().setZoneSpinner(
					_zonePickerView.getZoneSpinner());
			_providerPickerView.getProviderSpinner().setCountrySpinner(
					_countryPickerView.getCountrySpinner());

			addView(_zonePickerView);
			addView(_countryPickerView);
			addView(_providerPickerView);

			// setOrientation(TableLayout.VERTICAL);
			setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT));
		}
	}

	public DestinationView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i("DestinationView",
				"creating the DestinationView with AttributeSet");

		_context = context;
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		Log.i("DestinationView", "onLayout");
		super.onLayout(changed, l, t, r, b);
	}

	@Override
	protected void onAttachedToWindow() {
		Log.i("DestinationView", "onAttachedToWindow");
		super.onAttachedToWindow();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("DestinationView", "onDraw");
		super.onDraw(canvas);
	}

	@Override
	protected void onFinishInflate() {
		Log.i("DestinationView", "onFinishInflate");
		super.onFinishInflate();
		setDestinationView();
	}

	@Override
	public void updateViewLayout(View view,
			android.view.ViewGroup.LayoutParams params) {
		Log.i("DestinationView", "updateViewLayout");
		super.updateViewLayout(view, params);
	}

	public ZoneSpinner getZoneSpinner() {
		return _zonePickerView.getZoneSpinner();
	}

	public ProviderSpinner getProviderSpinner() {
		return _providerPickerView.getProviderSpinner();
	}

	public CountrySpinner getCountrySpinner() {
		return _countryPickerView.getCountrySpinner();
	}

	public Destination getDestination() {		
		return new Destination(getZoneSpinner().getSelectedZone(),getCountrySpinner().getSelectedCountry(), getProviderSpinner().getSelectedProvider());
	}

	public void setDestination(long destinationZoneId,
			long destinationCountryId, long destinationProviderId) {
		
		boolean set = false;
		int index = 0;
		
		while(index < getZoneSpinner().getZones().size())
		{
			if(getZoneSpinner().getZones().get(index).getId() == destinationZoneId)
			{
				getZoneSpinner().setSelection(index);
				set = true;
				index = getZoneSpinner().getZones().size();
			}
			
			index++;
		}
		
		if(!set)
		{
			throw new InvalidParameterException("could not set the zone " + destinationZoneId);
		}
		
		if(destinationCountryId != -1)
		{
			set = false;
			index = 0;
			
			while(index < getCountrySpinner().getCountries().size())
			{
				if(getCountrySpinner().getCountries().get(index).getId() == destinationZoneId)
				{
					getCountrySpinner().setSelection(index);
					set = true;
					index = getCountrySpinner().getCountries().size();
				}
				
				index++;
			}
			
			if(!set)
			{
				throw new InvalidParameterException("could not set the country " + destinationCountryId);
			}
			
			if(destinationProviderId != -1)
			{
				set = false;
				index = 0;
				
				while(index < getProviderSpinner().getProviders().size())
				{
					if(getProviderSpinner().getProviders().get(index).getId() == destinationZoneId)
					{
						getProviderSpinner().setSelection(index);
						set = true;
						index = getProviderSpinner().getProviders().size();
					}
					
					index++;
				}
				
				if(!set)
				{
					throw new InvalidParameterException("could not set the provider " + destinationProviderId);
				}
			}
		}
	}
}
